<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Admin\Service;

use Admin\Data\Common;
use Admin\Entity\SelfLocationMember;
use Doctrine\ORM\EntityManager;

class SelfLocationMemberManager
{
    private $entityManager;

    public function __construct(
        EntityManager $entityManager
    )
    {
        $this->entityManager = $entityManager;
    }

    /**
     * 添加核销人员
     * @param array $data
     * @return SelfLocationMember
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function addSelfLocationMember(array $data)
    {
        $member = new SelfLocationMember();
        $member->valuesSet($data);
        $member->setMemberAddTime(time());

        $this->entityManager->persist($member);
        $this->entityManager->flush();

        return $member;
    }

    /**
     * 编辑核销人员
     * @param array $data
     * @param SelfLocationMember $selfLocationMember
     * @return SelfLocationMember
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function editSelfLocationMember(array $data, SelfLocationMember $selfLocationMember)
    {
        if (isset($data['memberAccount'])) unset($data['memberAccount']);

        $selfLocationMember->valuesSet($data);
        $this->entityManager->flush();

        return $selfLocationMember;
    }

    /**
     * 重置密码
     * @param $password
     * @param SelfLocationMember $selfLocationMember
     * @return bool
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function changeMemberPassword($password, SelfLocationMember $selfLocationMember)
    {
        if (!Common::verifyUserPassword($password, $selfLocationMember->getMemberPassword())) {
            $selfLocationMember->setMemberPassword($password);
            $this->entityManager->flush();
        }

        return true;
    }

    /**
     * 删除核销人员
     * @param SelfLocationMember $selfLocationMember
     * @return SelfLocationMember
     * @throws \Doctrine\ORM\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function deleteSelfLocationMember(SelfLocationMember $selfLocationMember)
    {
        $this->entityManager->remove($selfLocationMember);
        $this->entityManager->flush();

        return $selfLocationMember;
    }
}